/*     */ package com.zimbra.soap;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.ElementFactory;
/*     */ import com.zimbra.common.soap.Element.JSONElement;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.soap.SoapTransport.DebugListener;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.TreeMap;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ import org.dom4j.Namespace;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SoapCommandUtil
/*     */   implements SoapTransport.DebugListener
/*     */ {
/*  60 */   private static final Map<String, Namespace> sTypeToNamespace = new TreeMap();
/*     */   
/*     */ 
/*  63 */   private static final String DEFAULT_ADMIN_URL = String.format("https://%s:%d/service/admin/soap", new Object[] { LC.zimbra_zmprov_default_soap_server.value(), Integer.valueOf(LC.zimbra_admin_service_port.intValue()) });
/*     */   
/*     */ 
/*  66 */   private static final String DEFAULT_URL = "http://" + LC.zimbra_zmprov_default_soap_server.value() + (LC.zimbra_mail_service_port.intValue() == 80 ? "" : new StringBuilder().append(":").append(LC.zimbra_mail_service_port.intValue()).toString()) + "/service/soap";
/*     */   
/*     */   private static final String LO_HELP = "help";
/*     */   
/*     */   private static final String LO_MAILBOX = "mailbox";
/*     */   
/*     */   private static final String LO_AUTH = "auth";
/*     */   
/*     */   private static final String LO_ADMIN_PRIV = "admin-priv";
/*     */   private static final String LO_ADMIN = "admin";
/*     */   private static final String LO_PASSWORD = "password";
/*     */   private static final String LO_PASSFILE = "passfile";
/*     */   private static final String LO_URL = "url";
/*     */   private static final String LO_ZADMIN = "zadmin";
/*     */   private static final String LO_VERBOSE = "verbose";
/*     */   private static final String LO_VERY_VERBOSE = "very-verbose";
/*     */   private static final String LO_NO_OP = "no-op";
/*     */   private static final String LO_SELECT = "select";
/*     */   private static final String LO_JSON = "json";
/*     */   private static final String LO_JAXB = "jaxb";
/*     */   private static final String LO_NO_JAXB = "no-jaxb";
/*     */   private static final String LO_FILE = "file";
/*     */   private static final String LO_TYPE = "type";
/*     */   private static final String LO_USE_SESSION = "use-session";
/*     */   private static final String TYPE_MAIL = "mail";
/*     */   private static final String TYPE_ADMIN = "admin";
/*     */   private static final String TYPE_ACCOUNT = "account";
/*     */   private static final String TYPE_IM = "im";
/*     */   private static final String TYPE_MOBILE = "mobile";
/*     */   private static final String TYPE_OFFLINE = "offline";
/*     */   private static final String TYPE_VOICE = "voice";
/*     */   
/*     */   static
/*     */   {
/* 100 */     sTypeToNamespace.put("mail", Namespace.get("urn:zimbraMail"));
/* 101 */     sTypeToNamespace.put("admin", Namespace.get("urn:zimbraAdmin"));
/* 102 */     sTypeToNamespace.put("account", Namespace.get("urn:zimbraAccount"));
/* 103 */     sTypeToNamespace.put("im", Namespace.get("urn:zimbraIM"));
/* 104 */     sTypeToNamespace.put("mobile", Namespace.get("urn:zimbraSync"));
/* 105 */     sTypeToNamespace.put("offline", Namespace.get("urn:zimbraOffline"));
/* 106 */     sTypeToNamespace.put("voice", Namespace.get("urn:zimbraVoice"));
/*     */   }
/*     */   
/* 109 */   private final Options mOptions = new Options();
/*     */   private String mUrl;
/*     */   private String mType;
/*     */   private Namespace mNamespace;
/*     */   private String mAuthAccountName;
/*     */   private String mAdminAccountName;
/*     */   private String mTargetAccountName;
/*     */   private String mPassword;
/*     */   private String[] mPaths;
/*     */   private String mAuthToken;
/*     */   private String mSessionId;
/*     */   private SoapHttpTransport mTransport;
/* 121 */   private boolean mVerbose = false;
/* 122 */   private boolean mVeryVerbose = false;
/* 123 */   private boolean mUseSession = false;
/* 124 */   private boolean mNoOp = false;
/*     */   private String mSelect;
/* 126 */   private boolean mUseJson = false;
/* 127 */   private boolean useJaxb = false;
/*     */   private String mFilePath;
/*     */   private Element.ElementFactory mFactory;
/*     */   private PrintStream mOut;
/*     */   
/*     */   SoapCommandUtil()
/*     */   {
/* 134 */     this.mOptions.addOption(new Option("h", "help", false, "Display this help message."));
/*     */     
/* 136 */     Option opt = new Option("m", "mailbox", true, "Send mail and account requests to this account.  Also used for authentication if --auth, -a and -z are not specified.");
/*     */     
/* 138 */     opt.setArgName("account-name");
/* 139 */     this.mOptions.addOption(opt);
/*     */     
/* 141 */     opt = new Option(null, "auth", true, "Account name to authenticate as.  Defaults to account in -m.");
/* 142 */     opt.setArgName("account-name");
/* 143 */     this.mOptions.addOption(opt);
/*     */     
/* 145 */     this.mOptions.addOption(new Option("A", "admin-priv", false, "Execute requests with admin privileges."));
/*     */     
/* 147 */     opt = new Option("a", "admin", true, "Admin account name to authenticate as.");
/* 148 */     opt.setArgName("account-name");
/* 149 */     this.mOptions.addOption(opt);
/*     */     
/* 151 */     opt = new Option("p", "password", true, "Password.");
/* 152 */     opt.setArgName("password");
/* 153 */     this.mOptions.addOption(opt);
/*     */     
/* 155 */     opt = new Option("P", "passfile", true, "Read password from file.");
/* 156 */     opt.setArgName("path");
/* 157 */     this.mOptions.addOption(opt);
/*     */     
/* 159 */     opt = new Option("u", "url", true, "SOAP service URL, usually http[s]://host:port/service/soap or https://host:port/service/admin/soap.");
/*     */     
/* 161 */     opt.setArgName("url");
/* 162 */     this.mOptions.addOption(opt);
/*     */     
/* 164 */     this.mOptions.addOption(new Option("z", "zadmin", false, "Authenticate with zimbra admin name/password from localconfig."));
/*     */     
/* 166 */     this.mOptions.addOption(new Option("v", "verbose", false, "Print the request."));
/*     */     
/* 168 */     this.mOptions.addOption(new Option("vv", "very-verbose", false, "Print URLs and all requests and responses with envelopes."));
/*     */     
/* 170 */     this.mOptions.addOption(new Option("n", "no-op", false, "Print the SOAP request only.  Don't send it."));
/*     */     
/* 172 */     opt = new Option(null, "select", true, "Select an element or attribute from the response.");
/* 173 */     opt.setArgName("xpath");
/* 174 */     this.mOptions.addOption(opt);
/*     */     
/* 176 */     this.mOptions.addOption(new Option(null, "json", false, "Use JSON instead of XML. (Switches on --jaxb option by default)."));
/*     */     
/*     */ 
/* 179 */     this.mOptions.addOption(new Option(null, "jaxb", false, "Force use of JAXB to aid building request from command line."));
/*     */     
/*     */ 
/* 182 */     this.mOptions.addOption(new Option(null, "no-jaxb", false, "Disallow use of JAXB to aid building request from command line."));
/*     */     
/*     */ 
/* 185 */     opt = new Option("f", "file", true, "Read request from file.  For JSON, the request pair should be the child of the root object.");
/*     */     
/* 187 */     opt.setArgName("path");
/* 188 */     this.mOptions.addOption(opt);
/*     */     
/* 190 */     String types = StringUtil.join(",", sTypeToNamespace.keySet());
/* 191 */     opt = new Option("t", "type", true, "SOAP request type: " + types + ".  Default is admin, or mail if -m is specified.");
/*     */     
/* 193 */     opt.setArgName("type");
/* 194 */     this.mOptions.addOption(opt);
/*     */     
/* 196 */     this.mOptions.addOption(new Option(null, "use-session", false, "Use a SOAP session."));
/*     */     try
/*     */     {
/* 199 */       this.mOut = new PrintStream(System.out, true, "utf-8");
/*     */     } catch (UnsupportedEncodingException e) {}
/*     */   }
/*     */   
/*     */   private void usage(String errorMsg) {
/* 204 */     int exitStatus = 0;
/*     */     
/* 206 */     if (errorMsg != null) {
/* 207 */       System.err.println(errorMsg);
/* 208 */       exitStatus = 1;
/*     */     }
/* 210 */     HelpFormatter format = new HelpFormatter();
/* 211 */     format.printHelp(new PrintWriter(System.err, true), 80, "zmsoap [options] xpath1 [xpath2 xpath3 ...]", null, this.mOptions, 2, 2, "Element paths roughly follow XPath syntax.  The path of each subsequent element is relative to the previous one.  To navigate up the element tree, use \"../\" in the path.  To specify attributes on the current element, use one or more @attr=val arguments.  To specify element text, use \"path/to/element=text\".\nExample: zmsoap -z GetAccountInfoRequest/account=user1 @by=name");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 219 */     System.exit(exitStatus);
/*     */   }
/*     */   
/*     */ 
/*     */   private void parseCommandLine(String[] args)
/*     */     throws ParseException
/*     */   {
/* 226 */     GnuParser parser = new GnuParser();
/* 227 */     CommandLine cl = parser.parse(this.mOptions, args);
/*     */     
/* 229 */     if (CliUtil.hasOption(cl, "help")) {
/* 230 */       usage(null);
/*     */     }
/*     */     
/*     */ 
/* 234 */     String val = CliUtil.getOptionValue(cl, "passfile");
/* 235 */     if (!StringUtil.isNullOrEmpty(val)) {
/* 236 */       String path = CliUtil.getOptionValue(cl, "passfile");
/*     */       try {
/* 238 */         this.mPassword = StringUtil.readSingleLineFromFile(path);
/*     */       } catch (IOException e) {
/* 240 */         usage("Cannot read password from file: " + e.getMessage());
/*     */       }
/*     */     }
/* 243 */     if (CliUtil.hasOption(cl, "password")) {
/* 244 */       this.mPassword = CliUtil.getOptionValue(cl, "password");
/*     */     }
/* 246 */     this.mAdminAccountName = CliUtil.getOptionValue(cl, "admin");
/*     */     
/* 248 */     if ((!CliUtil.hasOption(cl, "admin")) && (CliUtil.hasOption(cl, "zadmin"))) {
/* 249 */       this.mAdminAccountName = LC.zimbra_ldap_user.value();
/* 250 */       if (!CliUtil.hasOption(cl, "password")) {
/* 251 */         this.mPassword = LC.zimbra_ldap_password.value();
/*     */       }
/*     */     }
/*     */     
/* 255 */     this.mTargetAccountName = CliUtil.getOptionValue(cl, "mailbox");
/* 256 */     if ((CliUtil.hasOption(cl, "admin-priv")) && (CliUtil.hasOption(cl, "auth"))) {
/* 257 */       usage("Cannot combine --auth and -A.");
/*     */     }
/* 259 */     this.mAuthAccountName = CliUtil.getOptionValue(cl, "auth");
/* 260 */     if ((StringUtil.isNullOrEmpty(this.mAuthAccountName)) && (!CliUtil.hasOption(cl, "admin-priv"))) {
/* 261 */       this.mAuthAccountName = this.mTargetAccountName;
/*     */     }
/* 263 */     if ((StringUtil.isNullOrEmpty(this.mAuthAccountName)) && (StringUtil.isNullOrEmpty(this.mAdminAccountName))) {
/* 264 */       usage("Authentication account not specified.");
/*     */     }
/*     */     
/* 267 */     if (CliUtil.hasOption(cl, "type")) {
/* 268 */       this.mType = CliUtil.getOptionValue(cl, "type");
/* 269 */       if (!sTypeToNamespace.containsKey(this.mType)) {
/* 270 */         usage("Invalid type: " + this.mType);
/*     */       }
/* 272 */       if (((this.mType.equals("mail")) || (this.mType.equals("account")) || (this.mType.equals("im"))) && (StringUtil.isNullOrEmpty(this.mTargetAccountName)))
/*     */       {
/* 274 */         usage("Mailbox must be specified for mail, account, and im requests.");
/*     */       }
/*     */     }
/* 277 */     else if (!StringUtil.isNullOrEmpty(this.mTargetAccountName)) {
/* 278 */       this.mType = "mail";
/*     */     } else {
/* 280 */       this.mType = "admin";
/*     */     }
/*     */     
/* 283 */     this.mNamespace = ((Namespace)sTypeToNamespace.get(this.mType));
/*     */     
/* 285 */     this.mUrl = CliUtil.getOptionValue(cl, "url");
/* 286 */     if (StringUtil.isNullOrEmpty(this.mUrl)) {
/* 287 */       if (!StringUtil.isNullOrEmpty(this.mAdminAccountName)) {
/* 288 */         this.mUrl = DEFAULT_ADMIN_URL;
/*     */       } else {
/* 290 */         this.mUrl = DEFAULT_URL;
/*     */       }
/*     */     }
/*     */     
/* 294 */     this.mVeryVerbose = CliUtil.hasOption(cl, "very-verbose");
/* 295 */     this.mVerbose = CliUtil.hasOption(cl, "verbose");
/*     */     
/* 297 */     this.mPaths = cl.getArgs();
/* 298 */     this.mNoOp = CliUtil.hasOption(cl, "no-op");
/* 299 */     this.mSelect = CliUtil.getOptionValue(cl, "select");
/* 300 */     this.mUseJson = CliUtil.hasOption(cl, "json");
/* 301 */     if (CliUtil.hasOption(cl, "jaxb")) {
/* 302 */       this.useJaxb = true;
/* 303 */     } else if (CliUtil.hasOption(cl, "no-jaxb")) {
/* 304 */       this.useJaxb = false;
/*     */ 
/*     */ 
/*     */     }
/*     */     else
/*     */     {
/*     */ 
/* 311 */       this.useJaxb = this.mUseJson;
/*     */     }
/* 313 */     this.mFilePath = CliUtil.getOptionValue(cl, "file");
/* 314 */     this.mFactory = (this.mUseJson ? Element.JSONElement.mFactory : Element.XMLElement.mFactory);
/* 315 */     this.mUseSession = CliUtil.hasOption(cl, "use-session");
/*     */   }
/*     */   
/* 318 */   private static final String[] XPATH_PASSWORD = { "Body", AdminConstants.AUTH_REQUEST.getName(), "password" };
/*     */   
/*     */   public void sendSoapMessage(Element envelope)
/*     */   {
/* 322 */     if (this.mVeryVerbose)
/*     */     {
/* 324 */       Element passwordElement = envelope.getPathElement(XPATH_PASSWORD);
/* 325 */       String originalPassword = null;
/* 326 */       if (passwordElement != null) {
/* 327 */         originalPassword = passwordElement.getText();
/* 328 */         passwordElement.setText("***");
/*     */       }
/*     */       
/* 331 */       this.mOut.println(envelope.prettyPrint());
/*     */       
/* 333 */       if (passwordElement != null) {
/* 334 */         passwordElement.setText(originalPassword);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void receiveSoapMessage(Element envelope)
/*     */   {
/* 341 */     if (this.mVeryVerbose) {
/* 342 */       this.mOut.println(envelope.prettyPrint());
/*     */     }
/*     */   }
/*     */   
/*     */   private void adminAuth()
/*     */     throws ServiceException, IOException
/*     */   {
/* 349 */     Element auth = this.mFactory.createElement(AdminConstants.AUTH_REQUEST);
/* 350 */     auth.addElement("name").setText(this.mAdminAccountName);
/* 351 */     auth.addElement("password").setText(this.mPassword);
/*     */     
/*     */ 
/* 354 */     Element response = null;
/*     */     
/* 356 */     if (this.mVeryVerbose) {
/* 357 */       this.mOut.println("Sending admin auth request to " + this.mUrl);
/*     */     }
/*     */     
/* 360 */     response = getTransport(true).invoke(auth, false, !this.mUseSession, null);
/* 361 */     handleAuthResponse(response);
/*     */     
/*     */ 
/* 364 */     if ((!this.mType.equals("admin")) && (!StringUtil.isNullOrEmpty(this.mAuthAccountName))) {
/* 365 */       boolean nameIsUUID = StringUtil.isUUID(this.mAuthAccountName);
/*     */       
/* 367 */       Element getInfo = this.mFactory.createElement(AdminConstants.GET_ACCOUNT_INFO_REQUEST);
/* 368 */       Element account = getInfo.addElement("account").setText(this.mAuthAccountName);
/* 369 */       account.addAttribute("by", nameIsUUID ? "id" : "name");
/* 370 */       if (this.mVeryVerbose) {
/* 371 */         this.mOut.println(getInfo.prettyPrint());
/*     */       }
/* 373 */       response = getTransport(true).invoke(getInfo, false, !this.mUseSession, null);
/* 374 */       String userServiceUrl = response.getElement("soapURL").getText();
/*     */       
/*     */ 
/* 377 */       Element delegateAuth = this.mFactory.createElement(AdminConstants.DELEGATE_AUTH_REQUEST);
/* 378 */       account = delegateAuth.addElement("account").setText(this.mAuthAccountName);
/* 379 */       account.addAttribute("by", nameIsUUID ? "id" : "name");
/* 380 */       response = getTransport(true).invoke(delegateAuth, false, !this.mUseSession, null);
/* 381 */       handleAuthResponse(response);
/*     */       
/*     */ 
/* 384 */       this.mUrl = userServiceUrl;
/*     */     }
/*     */   }
/*     */   
/*     */   private SoapHttpTransport getTransport(boolean isAdmin) {
/* 389 */     if ((this.mTransport == null) || (!Objects.equal(this.mTransport.getURI(), this.mUrl))) {
/* 390 */       this.mTransport = new SoapHttpTransport(this.mUrl);
/*     */     }
/* 392 */     this.mTransport.setAdmin(isAdmin);
/* 393 */     this.mTransport.setAuthToken(this.mAuthToken);
/* 394 */     this.mTransport.setSessionId(this.mSessionId);
/* 395 */     this.mTransport.setDebugListener(this);
/* 396 */     this.mTransport.setTimeout(0);
/* 397 */     return this.mTransport;
/*     */   }
/*     */   
/*     */   private void handleAuthResponse(Element authResponse) throws ServiceException
/*     */   {
/* 402 */     this.mAuthToken = authResponse.getAttribute("authToken");
/* 403 */     Element sessionEl = authResponse.getOptionalElement("session");
/* 404 */     if (sessionEl != null) {
/* 405 */       this.mSessionId = sessionEl.getAttribute("id");
/*     */     }
/*     */   }
/*     */   
/*     */   private void mailboxAuth() throws ServiceException, IOException
/*     */   {
/* 411 */     if (this.mVeryVerbose) {
/* 412 */       this.mOut.println("Sending auth request to " + this.mUrl);
/*     */     }
/*     */     
/*     */ 
/* 416 */     Element auth = this.mFactory.createElement(AccountConstants.AUTH_REQUEST);
/* 417 */     Element account = auth.addElement("account").setText(this.mAuthAccountName);
/* 418 */     account.addAttribute("by", StringUtil.isUUID(this.mAuthAccountName) ? "id" : "name");
/* 419 */     auth.addElement("password").setText(this.mPassword);
/*     */     
/*     */ 
/* 422 */     Element response = getTransport(false).invoke(auth, false, !this.mUseSession, null);
/* 423 */     handleAuthResponse(response);
/*     */   }
/*     */   
/*     */   private Element buildRequestFromCommandLine() throws ServiceException
/*     */   {
/* 428 */     Element element = null;
/* 429 */     for (String path : this.mPaths) {
/* 430 */       element = processPath(element, path, this.useJaxb ? Element.XMLElement.mFactory : this.mFactory);
/*     */     }
/* 432 */     while (element.getParent() != null) {
/* 433 */       element = element.getParent();
/*     */     }
/* 435 */     if (this.useJaxb) {
/* 436 */       element = JaxbUtil.jaxbToElement(JaxbUtil.elementToJaxb(element), this.mFactory);
/*     */     }
/* 438 */     return element;
/*     */   }
/*     */   
/*     */   private void run()
/*     */     throws ServiceException, IOException
/*     */   {
/* 444 */     Element element = null;
/* 445 */     InputStream in = null;
/* 446 */     String location = null;
/* 447 */     if (this.mFilePath != null)
/*     */     {
/* 449 */       in = new FileInputStream(this.mFilePath);
/* 450 */       location = this.mFilePath;
/* 451 */     } else if (this.mPaths.length > 0) {
/* 452 */       element = buildRequestFromCommandLine();
/* 453 */     } else if (System.in.available() > 0)
/*     */     {
/* 455 */       in = System.in;
/* 456 */       location = "stdin";
/*     */     }
/*     */     
/* 459 */     if (in != null) {
/*     */       try {
/* 461 */         if (this.mUseJson) {
/* 462 */           element = Element.parseJSON(in);
/* 463 */           if ((element != null) && 
/* 464 */             (!element.getName().endsWith("Request"))) {
/* 465 */             Iterator<Element> iter = element.elementIterator();
/* 466 */             while (iter.hasNext()) {
/* 467 */               Element child = (Element)iter.next();
/* 468 */               if (child.getName().endsWith("Request")) {
/* 469 */                 child.detach();
/* 470 */                 element = child;
/* 471 */                 break;
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */         else {
/* 477 */           element = Element.parseXML(in);
/*     */         }
/*     */       } catch (IOException e) {
/* 480 */         System.err.format("Unable to read request from %s: %s.\n", new Object[] { location, e.getMessage() });
/* 481 */         System.exit(1);
/*     */       } finally {
/* 483 */         ByteUtil.closeStream(in);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 488 */     Element request = element;
/* 489 */     if (request == null) {
/* 490 */       usage("No request element specified.");
/*     */     }
/* 492 */     while (request.getParent() != null) {
/* 493 */       request = request.getParent();
/*     */     }
/*     */     
/* 496 */     if (this.mNoOp) {
/* 497 */       this.mOut.println(request.prettyPrint());
/* 498 */       return;
/*     */     }
/*     */     
/*     */ 
/* 502 */     if (this.mAdminAccountName != null) {
/* 503 */       adminAuth();
/*     */     } else {
/* 505 */       mailboxAuth();
/*     */     }
/*     */     
/*     */ 
/* 509 */     if ((!this.mType.equals("admin")) && (!StringUtil.isNullOrEmpty(this.mTargetAccountName)) && (!StringUtil.equalIgnoreCase(this.mAuthAccountName, this.mTargetAccountName)))
/*     */     {
/* 511 */       getTransport(false).setTargetAcctName(this.mTargetAccountName);
/*     */     }
/* 513 */     Element response = null;
/*     */     
/* 515 */     if (this.mVeryVerbose) {
/* 516 */       System.out.println("Sending request to " + this.mUrl);
/*     */     }
/* 518 */     if (this.mVerbose) {
/* 519 */       System.out.println(request.prettyPrint());
/*     */     }
/*     */     
/* 522 */     response = getTransport(this.mType.equals("admin")).invoke(request, false, !this.mUseSession, null);
/*     */     
/*     */ 
/* 525 */     Object results = null;
/* 526 */     String resultString = null;
/*     */     
/* 528 */     if (this.mSelect != null)
/*     */     {
/* 530 */       Element root = response.getFactory().createElement("root");
/* 531 */       response.detach();
/* 532 */       root.addElement(response);
/*     */       
/* 534 */       String[] parts = this.mSelect.split("/");
/* 535 */       if (parts.length > 0) {
/* 536 */         String lastPart = parts[(parts.length - 1)];
/* 537 */         if (lastPart.startsWith("@")) {
/* 538 */           parts[(parts.length - 1)] = lastPart.substring(1);
/* 539 */           resultString = root.getPathAttribute(parts);
/*     */         } else {
/* 541 */           results = root.getPathElementList(parts);
/*     */         }
/*     */       }
/*     */     } else {
/* 545 */       results = new ArrayList();
/* 546 */       ((List)results).add(response);
/*     */     }
/*     */     
/* 549 */     if (!this.mVeryVerbose) {
/* 550 */       if ((resultString == null) && (results != null)) {
/* 551 */         StringBuilder buf = new StringBuilder();
/* 552 */         boolean first = true;
/* 553 */         for (Element e : (List)results) {
/* 554 */           if (first) {
/* 555 */             first = false;
/*     */           } else {
/* 557 */             buf.append('\n');
/*     */           }
/* 559 */           buf.append(e.prettyPrint());
/*     */         }
/* 561 */         resultString = buf.toString();
/*     */       }
/* 563 */       if (resultString == null) {
/* 564 */         resultString = "";
/*     */       }
/* 566 */       this.mOut.println(resultString);
/*     */     }
/*     */   }
/*     */   
/* 570 */   private static Pattern PAT_PATH_AND_VALUE = Pattern.compile("([^=]*)=(.*)");
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Element processPath(Element start, String path, Element.ElementFactory factory)
/*     */   {
/* 586 */     String value = null;
/*     */     
/*     */ 
/* 589 */     Matcher m = PAT_PATH_AND_VALUE.matcher(path);
/* 590 */     if (m.matches()) {
/* 591 */       path = m.group(1);
/* 592 */       value = m.group(2);
/*     */     }
/*     */     
/*     */ 
/* 596 */     Element element = start;
/*     */     
/*     */ 
/* 599 */     String[] parts = path.split("/");
/* 600 */     String part = null;
/* 601 */     for (int i = 0; i < parts.length; i++) {
/* 602 */       part = parts[i];
/* 603 */       if (element == null) {
/* 604 */         QName name = QName.get(part, this.mNamespace);
/* 605 */         element = factory.createElement(name);
/* 606 */       } else if (part.equals("..")) {
/* 607 */         element = element.getParent();
/* 608 */       } else if (!part.startsWith("@")) {
/* 609 */         element = element.addElement(part);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 614 */     if ((value != null) && (part != null)) {
/* 615 */       if (part.startsWith("@")) {
/* 616 */         String attrName = part.substring(1);
/* 617 */         element.addAttribute(attrName, value);
/*     */       } else {
/* 619 */         element.setText(value);
/*     */       }
/*     */     }
/* 622 */     return element;
/*     */   }
/*     */   
/*     */   private static String formatServiceException(ServiceException e) {
/* 626 */     Throwable cause = e.getCause();
/* 627 */     return "ERROR: " + e.getCode() + " (" + e.getMessage() + ")" + (cause == null ? "" : new StringBuilder().append(" (cause: ").append(cause.getClass().getName()).append(" ").append(cause.getMessage()).append(")").toString());
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */   {
/* 632 */     CliUtil.toolSetup();
/* 633 */     SoapTransport.setDefaultUserAgent("zmsoap", null);
/* 634 */     SoapCommandUtil app = new SoapCommandUtil();
/*     */     try {
/* 636 */       app.parseCommandLine(args);
/*     */     } catch (ParseException e) {
/* 638 */       app.usage(e.getMessage());
/*     */     }
/*     */     try
/*     */     {
/* 642 */       app.run();
/*     */     } catch (ServiceException e) {
/* 644 */       System.err.println(formatServiceException(e));
/* 645 */       if (app.mVerbose) {
/* 646 */         e.printStackTrace(System.err);
/*     */       }
/* 648 */       System.exit(1);
/*     */     } catch (Exception e) {
/* 650 */       if ((app.mVerbose) || (app.mVeryVerbose)) {
/* 651 */         e.printStackTrace(System.err);
/*     */       } else {
/* 653 */         System.err.println(e);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/soap/SoapCommandUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */